/***
 * [open](https://wizardforcel.gitbooks.io/linux-c-api-ref/content/143.html)
 */

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include <string>

int main() {
    int fd;
    std::string str = "Hello  Linux mmap !\n";
    fd = open("/mnt/g/projects/cpp/VS/myFilesystem/test/mmapDemo/tmp", O_RDWR);
    write(fd, str.data(), str.size() + 1);
    struct stat sb;
    fstat(fd, &sb);
    void* pStart =
        mmap(
            nullptr, sb.st_size, 
            PROT_READ | PROT_WRITE, MAP_PRIVATE, 
            fd, 0);
    if (pStart == MAP_FAILED) {
        std::cout << u"mmap失败，可能是权限问题\n";
        return -1;
    }
    char* p = (char*)pStart;
    *(p + 5) = ',';
    std::cout << (char*)pStart;
    munmap(pStart, sb.st_size);
    close(fd);
    return 0;
}